<!--
 * @Author: linZeJian
 * @Date: 2022-08-24 10:53:55
 * @LastEditors: linZeJian
 * @LastEditTime: 2022-08-26 14:49:02
 * @Description:
-->
<template>
  <div v-if="column.display">
    <van-field
            clickable
            :name="column.prop"
            input-align="right"
            right-icon="arrow"
            v-model="showDate"
            :label="column.label"
            :placeholder="
        column.placeholder ? column.placeholder : '请选择' + column.label
      "
            :disabled="column.disabled"
            :required="column.required"
            :style="
        column.readonly || column.disabled || column.detail
          ? 'pointer-events: none;'
          : ''
      "
            :rules="[{ required: column.required }]"
            @click="show = true"
            readonly
    />
    <van-popup v-model:show="show"  round position="bottom">

      <van-date-picker
              v-model="currentValue"

              title="选择日期"
              :min-date="column.minDate"
              :max-date="column.maxDate"
              :columns-type="columnsType"
              @cancel="cancelDatetime"
              @confirm="confirmDatetime"

      />

    </van-popup>
  </div>
</template>

<script>

  import mixEvent from './mixins/mixEvent';
  import { formatDateToVants,formatToDateVants,getWeekNumber} from '@/framework-core/utils/util'
  export default {
    name: "avant-date",
    props: ['column','modelValue'],
    inject: ['getViewFormData'],
    mixins: [mixEvent],
    data() {
      return {
        show: false,
        columnsType: ['year']
      };
    },
    created() {

    },
    computed: {
      columnsType () {
        return  this.column.dateType
      },
      showDate () {
        if(this.column.dateformat=="week"){
          return  getWeekNumber(new Date(this.modelValue));
        }
        return  this.modelValue;
      },
    },

    mounted(){
      if( this.getViewFormData){
        this.formData = this.getViewFormData()
      }
    },

    watch: {
      modelValue: {
        handler(newVal, oldVal) {
          if (!newVal) {
            return
          }
          this.currentValue = formatToDateVants(newVal,this.column.dateformat);
        },
        immediate: true // 确保初始化时也执行一次
      }
    },

    methods: {

      /**
       * @description: 关闭时间弹窗
       * @param {*}
       * @return {*}
       */
      cancelDatetime() {
        this.show = false;
      },

      /**
       * @description: 时间选择确认
       * @param {*} value
       * @return {*}
       */
      confirmDatetime(value) {

        let v =  formatDateToVants(value.selectedValues,this.column.dateformat);
        this.$emit('update:modelValue',  v);
        this.bindCalculatorEvent(this.column,v);
        this.show = false;
      },

    },
  };
</script>

<style scoped>

</style>
